package cn.Mmovie.servlet;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.bean.Movie;
import cn.Mmovie.bean.Schedule;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.service.CinemaService;
import cn.Mmovie.service.MovieService;
import cn.Mmovie.service.ScheduleService;
import cn.Mmovie.service.impl.AuditoriumServiceImpl;
import cn.Mmovie.service.impl.CinemaServiceImpl;
import cn.Mmovie.service.impl.MovieServiceImpl;
import cn.Mmovie.service.impl.ScheduleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AdminScheduleModifyServlet", value = "/admin/schedule_modify")
public class AdminScheduleModifyServlet extends HttpServlet {
    private MovieService movieService=new MovieServiceImpl();
    private AuditoriumService auditoriumService=new AuditoriumServiceImpl();
    private ScheduleService scheduleService=new ScheduleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("---------ScheduleModify--------");
        if (request.getParameter("id")==null||request.getParameter("id")==""){
            request.setAttribute("failMsg","没有指定需要修改的放映计划，请重试！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }
        Integer id=Integer.parseInt(request.getParameter("id"));
        Schedule schedule=scheduleService.queryById(id);
        if (schedule!=null){
            List<Movie> movies=movieService.queryAllMovies();
            List<Auditorium> auditoriums=auditoriumService.queryAllAuditorium();
            request.setAttribute("movies",movies);
            request.setAttribute("auditoriums",auditoriums);
            request.setAttribute("schedule",schedule);
            request.getRequestDispatcher("/WEB-INF/page/main/schedule_modify.jsp").forward(request,response);
        }else {
            request.setAttribute("failMsg","需要修改的放映计划不存在，请重试！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }
    }
}
